package com.funtl.screw.admin.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.dto.SysLogDTO;
import com.funtl.screw.admin.entity.SysLog;
import com.funtl.screw.admin.vo.PreLogVO;
import com.funtl.screw.admin.service.SysLogService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author Lusifer
 * @since 2017-11-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/log")
@Api(value = "log", tags = "日志管理模块")
public class SysLogController {

    private final SysLogService sysLogService;

    /**
     * 简单分页查询
     */
    @GetMapping("/page")
    public ResponseResult<Page<SysLog>> getLogPage(QueryRequest queryRequest, SysLogDTO sysLog) {
        Page<SysLog> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        return ResponseResult.ok(sysLogService.getLogByPage(page, sysLog));
    }

    /**
     * 删除日志
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public ResponseResult<Boolean> removeById(@PathVariable Long id) {
        return ResponseResult.ok(sysLogService.removeById(id));
    }

    /**
     * 批量删除日志
     *
     * @param ids ID
     * @return success/false
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public ResponseResult<Boolean> removeByIds(@RequestBody Long[] ids) {
        return ResponseResult.ok(sysLogService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 插入日志
     *
     * @param sysLog 日志实体
     * @return success/false
     */
    @Inner
    @PostMapping("/save")
    public ResponseResult<Boolean> save(@Valid @RequestBody SysLogDTO sysLog) {
        return ResponseResult.ok(sysLogService.saveLog(sysLog));
    }

    /**
     * 批量插入前端异常日志
     *
     * @param preLogVoList 日志实体
     * @return success/false
     */
    @PostMapping("/logs")
    public ResponseResult<Boolean> saveBatchLogs(@RequestBody List<PreLogVO> preLogVoList) {
        return ResponseResult.ok(sysLogService.saveBatchLogs(preLogVoList));
    }
}
